/*
 *  Copyright (C) 1993  Robert Davis
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of Version 2, or any later version, of 
 *  the GNU General Public License as published by the Free Software 
 *  Foundation.
 */

static char RCSId[]="$Id: SubCell.m,v 1.5 1993/05/04 16:22:56 davis Exp $";


/*  
 *  Based on CustomCell in the NeXT Developer Example ScrollDoodScroll 
 *  by Jayson Adams.
 */

#import <appkit/Font.h>
#import <appkit/NXImage.h>
#import <appkit/Text.h>		/* NXTextFontInfo()		*/

#import <dpsclient/wraps.h>

#import "SubCell.h"
#import "SubObject.h"


#define FIRST_COLUMN_START 4.0
#define TEXT_COLUMN_START 20.0


@implementation SubCell

static id 	attachmentImage = nil;
static NXSize	imageSize;


+ initialize
{
    /* Get the "attachment" image -- like the image in NeXTMail */
    if (!attachmentImage) {
	attachmentImage = [NXImage findImageNamed:"Attachment"];
	[attachmentImage getSize:&imageSize];
    }

    return self;
}


- init
{
    [super init];
    [self setStringValue:""];
    return self;
}


- setFont:fontObj
{
    [super setFont:fontObj];
    /*
     *  Save this info so we don't have to look it up every time we 
     *  draw.  Note:  "support" for a text Cell is a font object.
     */
    NXTextFontInfo(support, &ascender, &descender, &lineHeight);

    return self;
}



- drawInside:(const NXRect *)cellFrame inView:controlView
{
    NXPoint	imageOrigin;
    NXRect	rectArray[2];
    
    /*  Set the font according to our drawing status.  */
    if (NXDrawingStatus == NX_DRAWING)
	[[support screenFont] set];
    else
	[support set];
    

    /*  Erase the cell.  */
    PSsetgray((cFlags1.state || cFlags1.highlighted) ? NX_WHITE : NX_LTGRAY);
    NXRectFill(cellFrame);

    /*  Draw the "attachment" image, if the SubObject wants us to.  */
    if (showAttachment) {
	imageOrigin.x = FIRST_COLUMN_START;
	imageOrigin.y = NX_Y(cellFrame) + NX_HEIGHT(cellFrame) - 1
		        - (NX_HEIGHT(cellFrame) - imageSize.height) / 2.0;
	[attachmentImage composite:NX_PLUSD toPoint:&imageOrigin];
    }
      

    /*  Draw the text.  */
    PSsetgray(NX_BLACK);
    PSmoveto(NX_X(cellFrame) + TEXT_COLUMN_START,
    	     NX_Y(cellFrame) + lineHeight - descender);
    PSshow(contents);
    

    /*  Draw the two dark gray lines above and below the cell.  */
    PSsetgray(NX_DKGRAY);
    if (cFlags1.state || cFlags1.highlighted) {
	/*
	 *  Draw 1-pixel-high rectangles instead of lines (this is 
	 *  faster than PSmoveto(); PSlineto()).
	 */
	NXSetRect(&(rectArray[0]), NX_X(cellFrame), NX_Y(cellFrame),
		  NX_WIDTH(cellFrame), 1.0);
	NXSetRect(&(rectArray[1]), NX_X(cellFrame), NX_MAXY(cellFrame) - 1.0,
		  NX_WIDTH(cellFrame), 1.0);

	/*  
	 *  Using NXRectFillList is faster than separate calls to 
	 *  NXRectFill.
	 */
	NXRectFillList(rectArray, 2);
    }

    return self;
}



/*  
 *  This class implements the SubCell category of Cell.  Every cell 
 *  that does so has associated with it an object which stores the 
 *  information that the Cell displays.  When the sub object is set,
 *  we set our instance variables to match it.
 */
- setSubObject:anObject
{
    if (anObject) {
	const char *aString;

	subObject = anObject;
	aString = [subObject stringValue];
	[self setStringValue: aString? aString: ""];
	showAttachment = [subObject isAttachment];

	return self;

    } else
	return nil;
}


- subObject
{
    return subObject;
}



// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}


@end
